﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Ordinaire.Db;
using Ordinaire.Taxonomy.Data;

namespace Ordinaire.Taxonomy
{
    /// <summary>
    /// Represents a class responsible for managing any vocabs from SQL data source.
    /// </summary>
    public class VocabDataManager : DataManager<OContext>
    {
        /// <summary>
        /// Retrieves the data with the specified vocabulary.
        /// </summary>
        /// <param name="name">the vocabulary</param>
        /// <returns>the data of the specified vocabulary</returns>
        public Vocabs GetByName(string name)
        {
            return GetByName<Vocabs>(name);
        }

        /// <summary>
        /// Returns the data with the specified name.
        /// </summary>
        /// <typeparam name="TVocab">the table representation of Vocabs</typeparam>
        /// <param name="name">the vocabulary</param>
        /// <returns>the data of the specified vocabulary</returns>
        public TVocab GetByName<TVocab>(string name)
            where TVocab : Vocabs
        {
            name.ThrowIfNullOrEmpty<ArgumentException>(String.Format(ExceptionMessage.IsNullOrEmpty, "name"));
            return base.GetByName<TVocab, int, string>(name);
        }

        /// <summary>
        /// Creates new vocabulary into the data source.
        /// </summary>
        /// <param name="name">the vocabulary to store</param>
        /// <returns>the new vocabulary data</returns>
        public Vocabs Insert(string name)
        {
            return Insert<Vocabs>(name);
        }

        /// <summary>
        /// Creates new vocabulary into the specified table type.
        /// </summary>
        /// <typeparam name="TVocab">the table representation of Vocabs</typeparam>
        /// <param name="name">the vocabulary to store</param>
        /// <returns>the new vocabulary data</returns>
        public TVocab Insert<TVocab>(string name)
            where TVocab : Vocabs
        {
            name.ThrowIfNullOrEmpty<ArgumentException>(String.Format(ExceptionMessage.IsNullOrEmpty, "name"));
            name = name.TrimExtraSpaces();

            TVocab data = typeof(TVocab).Instance<TVocab>();
            data.Name   = name;
            return base.Insert<TVocab, int, string>(data)[0];
        }
    }
}
